<?php
/**
 * 监控服务 ws http 8501
 * User: Administrator
 * Date: 2018/8/1
 * Time: 16:28
 */
class Server
{
    const  PORT = 8501;

    public function port()
    {
        $shell = "netstat -anp 2>/dev/null | grep ".self::PORT." | grep LISTEN | wc -l";

        $result = shell_exec($shell);
        if ($result != 1) {
            //发送报警邮件，短信
            echo date("Ymd H:i:s").' error'.PHP_EOL;
        } else {
            echo date("Ymd H:i:s").' success'.PHP_EOL;
        }
    }
}

//swoole的定时器，每两秒执行
swoole_timer_tick(2000,function($timer_id){
    (new Server())->port();
});
